﻿Imports System.Windows.Forms
Public Class TerrainData

    '内部字段
    Dim originPoint As New MapWinGIS.Point '原点的绝对坐标,目前暂时使用原点
    Dim RefSWPoint As New MapWinGIS.Point '左下角的相对坐标
    Dim RefNEPoint As New MapWinGIS.Point '右上角的相对坐标

    Dim SWPoint As New MapWinGIS.Point '左下角的绝对坐标，需要通过鼠标或者指定



    '使用第二代代码
    Public m_manager As LayerManager



   
    '事件模型
    Public Event BoundingChanged(ByVal pWS As MapWinGIS.Point, ByVal pNE As MapWinGIS.Point)
    Public Event NewPointSeted(ByVal p As MapWinGIS.Point)


    '定义外部接口
    Public Sub AddDemLayer(ByVal ln As String, ByVal fn As String)

        'Dim img As New MapWinGIS.Image
        'img.Open(fn)
        'img.ImageColorScheme = MapWinGIS.PredefinedColorScheme.SummerMountains

        ''去除部分机器由此产生的bug
        'img.UseTransparencyColor = False
        'Dim iLayer As Integer

        'iLayer = mapMain.AddLayer(img, True)

        'mapMain.set_LayerKey(iLayer, ln)

        Dim img As New IMGGEOFileLayer(ln, fn)

        m_manager.AddLayer(img)
        img.UseTransParency = False
        m_manager.MoveLayerBottom(img.GetLayerPos())


    End Sub
    Public Sub RemoveDemLayer(ByVal ln As String)
        'Dim iLayer As Integer = Me.getLayerHandle(ln)

        'If iLayer <> -1 Then


        '    mapMain.RemoveLayer(iLayer)

        'End If

        m_manager.RemoveLayer(ln)

    End Sub
  

    '改进后的取得层句柄方法
    Private Function getLayerHandle(ByVal ln As String) As Integer
        If mapMain.NumLayers > 0 Then

            For i As Integer = 0 To mapMain.NumLayers - 1

                Dim iLayer As Integer = mapMain.get_LayerHandle(i)
                If mapMain.get_LayerKey(iLayer) = ln Then

                    Return iLayer

                End If


            Next




        End If

        Return -1


    End Function
    Private Function getUIntFromColor(ByVal clr As Drawing.Color) As UInt32

        Return RGB(clr.R, clr.G, clr.B)

    End Function

    Public Sub DrawBounds(ByVal pts() As MapWinGIS.Point, ByVal clr As Drawing.Color)
        Me.RemoveBound()


        Dim lay As ESPolylineLayer
        m_manager.GetLayer("bound", lay)

        Dim shpIndex As Integer = -1
        If lay IsNot Nothing Then

            shpIndex = lay.AddEntity("boundary", pts, "边界")

        End If

        ' Dim ucl As UInt32 = RGB(clr.R, clr.G, clr.B)

        lay.LineColorProperty = clr



    End Sub

    Public Sub DrawGrid(ByVal pts() As MapWinGIS.Point, ByVal clr As Drawing.Color)

        Me.RemoveGrid()

        Dim lay As ESPolylineLayer
        m_manager.GetLayer("grid", lay)

        Dim shpIndex As Integer = -1

        If lay IsNot Nothing Then

            shpIndex = lay.AddEntity("grid", pts, "网格")

        End If

        '  Dim ucl As UInt32 = RGB(clr.R, clr.G, clr.B)

        lay.LineColorProperty = clr


    End Sub

    Public Sub SetMNormal()
        m_manager.SetMNormal()
    End Sub

    Public Sub SetMDrawRect()

        m_manager.SetAllLayerNoEdditable()

        Dim cpLayer As ESPolylineLayer
        m_manager.GetLayer("bound", cpLayer)
        If cpLayer IsNot Nothing Then

            cpLayer.SetDrawEntity(True)
        End If

    End Sub



    Public Sub SetMDrawPoint()
        m_manager.SetAllLayerNoEdditable()

        Dim cpLayer As ESPointLayer
        m_manager.GetLayer("point", cpLayer)
        If cpLayer IsNot Nothing Then

            cpLayer.SetDrawEntity(True)
        End If

    End Sub

    Public Sub RemoveGrid()
        Dim lay As ESPolylineLayer
        m_manager.GetLayer("grid", lay)

        lay.RemoveEntity("grid")
    End Sub

    Public Sub RemoveBound()

        Dim lay As ESPolylineLayer
        m_manager.GetLayer("bound", lay)

        lay.RemoveEntity("boundary")



    End Sub




    Public Sub InitControl(ByVal str As String)

        'Dim str As String = System.IO.Path.GetTempPath()

        str += "\" + "Temp\"

        If Not System.IO.Directory.Exists(str) Then
            System.IO.Directory.CreateDirectory(str)
        End If

        'Dim tempDir As String
        'Dim now As DateTime = DateTime.Now
        'Dim rd As New Random

        'tempDir = now.ToString()

        'tempDir += tempDir + rd.NextDouble()
        'tempDir = tempDir.Replace(" ", "")
        'tempDir = tempDir.Replace("-", "")
        'tempDir = tempDir.Replace(":", "")
        'tempDir = tempDir.Replace(".", "")



        m_manager = New LayerManager(mapMain, str)
        AddHandler m_manager.AskForAddEntity, AddressOf Me.OnAskAdd


        Dim lay2 As New ESPointLayer("point", New PointTool)
        m_manager.AddLayer(lay2)

        Dim lay3 As New ESPolylineLayer("grid", New ParaRectangleTool)

        m_manager.AddLayer(lay3)

        Dim lay4 As New ESPolylineLayer("bound", New ParaRectangleTool)

        m_manager.AddLayer(lay4)




    End Sub



    Private Sub TerrainData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       
 


    End Sub

    Private Sub OnAskAdd(ByVal ln As String, ByVal pt As MapWinGIS.Point(), ByVal shpDesc As String)

        If ln = "bound" Then

            Me.RemoveBound()

            Me.DrawBounds(pt, Drawing.Color.Red)
            OnEndDrawing(pt)

        End If

        If ln = "point" Then

            RaiseEvent NewPointSeted(pt(0))

        End If






    End Sub


    Protected Sub OnEndDrawing(ByVal ptlist() As MapWinGIS.Point)
        ' Protected Sub OnEndDrawing(ByVal ptlist As List(Of Drawing.Point))

        Dim pWS As New MapWinGIS.Point
        Dim pNE As New MapWinGIS.Point

        Dim xMin As Double = Double.MaxValue
        Dim yMin As Double = Double.MaxValue

        Dim xMax As Double = Double.MinValue
        Dim yMax As Double = Double.MinValue




        Dim n As Integer = ptlist.Length

        '至少有四个点啊，哈哈
        If n < 4 Then
            Return
        End If


        'For Each pt As Drawing.Point In ptlist
        '    Dim ptemp As MapWinGIS.Point

        For Each ptemp As MapWinGIS.Point In ptlist


            ' mapMain.PixelToProj(pt.X, pt.Y, ptemp.x, ptemp.y)

            If ptemp.x < xMin Then
                xMin = ptemp.x
            End If

            If ptemp.x > xMax Then
                xMax = ptemp.x
            End If

            If ptemp.y < yMin Then

                yMin = ptemp.y
            End If

            If ptemp.y > yMax Then
                yMax = ptemp.y
            End If


        Next

        pWS.x = xMin
        pWS.y = yMin

        pNE.x = xMax
        pNE.y = yMax

        RaiseEvent BoundingChanged(pWS, pNE)



    End Sub


    Public Sub UnloadAllLayer()

        m_manager.UnloadAllLayer()

    End Sub

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

End Class
